The term platform is used to refer to a processor/hardware and operating system combination. Binary translators allow binaries built for a particular platform (source) to execute on another platform (target) by translating the machine instructions present in the binary code into equivalent instructions of the target platform. Binary translation can be performed either statically, wherein a new binary image is created for running directly on the target platform, or dynamically wherein the translation takes place while the binary is executed. Different types of binary translators are described by Erik R. Altman, David Kaeli, and Yaron Sheffer, in “Welcome to the Opportunities of Binary Translation.”, IEEE Computer, 33 (3), March 2000, pp 40-45.
Dynamic binary translators (DBTs) follow the execution path of the source binary, perform on-the-fly translation, store the translated code in a translation cache and finally execute the translated code. The translation may be stored in memory, as described by C. Zheng and C. Thompson in “PA-RISC to IA-64: Transparent execution, no recompilation.”, IEEE Computer, 33(3):47-52, March 2000. Alternatively, the translation may be stored on disk for use across executions of the same binary as described by R. J. Hookway and M. A. Herdeg.in “Digital FX!32: Combining emulation and binary translation.”, Digital Technical Journal, 9(1):3-12, 1997.
Conventional DBTs translate machine instructions from source to target platform using the following phases:                Decode the input instruction        Select target instructions        Optimize the selected instructions        Emit the optimized code into translation cache        
The translated code produced by such DBTs is native to the processor on which it is to be run and as such, can provide performance close to native compiled binaries. However, for this to be the case the cost of translation, which is an overhead on the runtime of the binary needs to be minimised while not compromising on quality of generated code. This is discussed further by M. Probst, in “Fast machine-adaptable dynamic binary translation.”, Proceedings of the Workshop on Binary Translation, Barcelona, Spain, September 2001.
The cost of translation can be high if frequently executed or “hot” segments of code are not translated into native code. Most known binary translators use a combination of emulation and translation to determine “hotness” of various parts of the binary, switching from emulation to translation at a threshold point in an attempt to make the process more efficient.
DBTs are commonly used as a migration tool and to run a large variety of applications. However, many applications have poor code locality and cause frequent extraneous events that expose translation overhead as the most significant performance bottleneck. One such event is the flushing of the translation cache. A DBT may be forced to flush the translation cache whenever the emulated application loads or unloads new shared libraries, encounters a Flush Cache (FIC) instruction (e.g. dynamic loader, Java Virtual Machine), or when the translation cache is full. Frequently flushing the translation cache leads to re-translation of hot segments of code increasing the total translation overhead.
Frederick Smith, Dan Grossman, Greg Morrisett, Luke Homof, & Trevor Jim, “Compiling for run-time code generation (extended version).”, Technical Report, October 2000, Department of Computer Science, Cornell University, describes the Cyclone programming language that provides explicit support for dynamic specialization of Cyclone routines using precompiled templates and a facility for optimising the templates. Also, Masuhara, H. and Yonezawa, A. “Run-time Bytecode Specialization: A Portable Approach to Generating Optimized Specialized Code.”, Lecture Notes in Computer Science, 2001, describes a run-time technique that operates on bytecode programs in Java Virtual Machine Language. Using this technique the Java bytecode programs are specialized and optimised at runtime. Both these efforts exploit the greater information available at runtime to specialize (or partially evaluate) the program thereby improving performance. However, such optimizations are opportunistic and cannot be applied for all input situations.
A significant part of the binary translation process is the transformation of data from the source instruction to the target instruction in a process referred to as bit filling. In known translators, whenever bit filling is carried out in high level code, the process typically involves the following steps:                a) Shift the required bits into the target position.        b) Mask out those bits, which are not required        c) The bits in the target corresponding to those bit positions where the new bits have to be copied into need to be cleared.        d) The source bits are copied into the target by an OR binary operation.        
If the source instruction bit sequence is such that it needs to be split up and copied into different positions in the target, then the steps above need to be carried out for each subset of bits that are to be copied into a unique location in the target. This results in a significant performance loss. Also, when the DBT is translating code from one format to another, the source bit patterns may need to be split and copied into various target instruction slots, thereby further reducing performance.
Another known solution to bit filling is inline assembly, which is provided by some compilers. This facility enables the user to code efficient assembly routines into the otherwise high-level language code. However, this solution is platform specific, and requires the programmer to know the instruction set architecture of every processor on which the binary has to execute. Furthermore, inline assembly can restrict possible compiler optimizations.
In summary, decoding and analysis of source instructions using traditional DBTs incurs a significant performance overhead resulting in the translated programs running less efficiently.
It is an object of the present invention to provide a method and apparatus for translating binary code, which avoids some of the above disadvantages or at least provides the public with a useful choice.